package com.fch.sdlibrary.controller;

import com.fch.sdlibrary.api.BaseResponse;
import com.fch.sdlibrary.entity.User;
import com.fch.sdlibrary.service.BookService;
import com.fch.sdlibrary.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 *
 */
@CrossOrigin
@RestController
public class UserController {
    private BaseResponse baseResponse;
    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;

    @PostMapping("Register")
    public BaseResponse insertUser(@RequestBody User user) {
        baseResponse = new BaseResponse();
        //加密用户的密码
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        user.setUserPasw(bcryptPasswordEncoder.encode(user.getPassword()));
        User fullUser = userService.insertUser(user);
        if (fullUser != null) {
            baseResponse.setData(fullUser);
            baseResponse.setCode(200);
        }
        return baseResponse;
    }

    //在控制器中获得当前用户名
    @GetMapping("username")
    public String currentUserName(Principal principal) {
        return principal.getName();
    }    //在控制器中获得当前用户名


    @RequestMapping("NeedLogin")
    public BaseResponse needLogin() {
        baseResponse = new BaseResponse();
        baseResponse.setCode(401);
        baseResponse.setMsg("需要登录");
        return baseResponse;
    }

    @PostMapping("Borrow")
    public BaseResponse borrow(@RequestParam String bookId,
                               Principal principal) {
        baseResponse = new BaseResponse();
        if (userService.hasBorrow(principal.getName()) != null) {
            baseResponse.setMsg("借阅失败,已有借阅的图书");
            baseResponse.setCode(200);
            return baseResponse;
        }

        if (!bookService.canBorrow(bookId)) {
            baseResponse.setMsg("借阅失败,库存不足");
            baseResponse.setCode(200);
            return baseResponse;
        }
        if (userService.doBorrow(bookId, principal.getName())) {
            baseResponse.setMsg("成功借阅" + "《" + bookService.queryBookById(bookId).getBookName() + "》");
            baseResponse.setCode(200);
            return baseResponse;
        }
        baseResponse.setMsg("借阅失败");
        return baseResponse;
    }

    @PostMapping("Remand")
    public BaseResponse remand(Principal principal) {
        baseResponse = new BaseResponse();
        if (userService.hasBorrow(principal.getName()) == null) {
            baseResponse.setMsg("没有借书");
            baseResponse.setCode(200);
            return baseResponse;
        }
        if (userService.doRemand(principal.getName())) {
            baseResponse.setMsg("还书成功");
            baseResponse.setCode(200);
            return baseResponse;
        }
        baseResponse.setMsg("还书失败");
        return baseResponse;
    }
}
